/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ipfw.cs590.cheat.struts.actions;

import edu.ipfw.cs590.cheat.AssignmentManagementInterface;
import edu.ipfw.cs590.cheat.AssignmentManagementService;
import edu.ipfw.cs590.cheat.struts.ForwardParameters;
import edu.ipfw.cs590.cheat.struts.actionforms.AssignmentManagementForm;
import edu.ipfw.cs590.cheat.util.Authentication;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author SafarTimuro
 */
public class ReplaceAssignment extends Action {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";

    /**
     * Replace an assignment with a new one.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        //If we have the right form type
        if (form instanceof AssignmentManagementForm) {
            AssignmentManagementForm amf = (AssignmentManagementForm) form;


            try {
                // Call Web Service Operation
                AssignmentManagementService service = new AssignmentManagementService();
                AssignmentManagementInterface port = service.getAssignmentManagementSOAP();
                // TODO initialize WS operation arguments here
                edu.ipfw.cs590.cheat.ReplaceAssignment parameters = new edu.ipfw.cs590.cheat.ReplaceAssignment();
                parameters.setUUID(amf.getId());
                parameters.setArchive(new JAXBElement(new QName("Archive"),
                        amf.getFile().getFileData().getClass(),
                        amf.getFile().getFileData()));
                String token = (String) request.getSession(false).getAttribute(Authentication.SESSION_NAME_TOKEN);

                //Call out
                String result = port.replaceAssignment(parameters, token).getResult();

                //Good stuff we got where we need
                ActionMessages errors = new ActionMessages();
                errors.add("success", new ActionMessage("success.service.replaceAssignment", amf.getId(), result));
                saveMessages(request, errors);

                return new ForwardParameters().add("id", result).forward(mapping.findForward(SUCCESS));
            } catch (Exception exc) {
                //Something bad happened on the webservice side fail.
                ActionMessages errors = new ActionMessages();
                errors.add(ActionMessages.GLOBAL_MESSAGE,
                        new ActionMessage("errors.service.error", "Replace Assignment", exc.getMessage()));
                this.saveErrors(request, errors);

                //Return failure.
                return new ForwardParameters().add("id", amf.getId()).forward(mapping.findForward(SUCCESS));
            }
        }

        //Something bad happened on the webservice side fail.
        ActionMessages errors = new ActionMessages();
        errors.add(ActionMessages.GLOBAL_MESSAGE,
                new ActionMessage("error.form.invalid"));
        this.saveErrors(request, errors);

        return mapping.getInputForward();
    }
}
